package org.example.basic.day07;

import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;

import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @author 清倌
 */
public class Demo01Stream流 {
    public static void main(String[] args) {
        List<Student> list=new ArrayList<>();
        list.add(new Student("张三", 18));
        list.add(new Student("lisi", 19));
        list.add(new Student("王五", 20));
        list.add(new Student("lisi", 19));

        list.stream().filter((student) -> student.getAge()>18).forEach(System.out::println);

        //搜集全部名字
        List<String> list1=list.stream().map((student) -> student.getName()).collect(Collectors.toList());
        System.out.println(list1);

        //根据年龄进行排序
        list.stream().sorted(Comparator.comparing((student -> student.getAge()))).forEach(System.out::println);

        //根据年龄进行分组
       Map<Integer,List<Student>> list2=list.stream().collect(Collectors.groupingBy(student->student.getAge()));
       list2.forEach((k,v)->{
           System.out.println("key = " + k + ", value = " + v);
       });
    }
}

@Data
@AllArgsConstructor
@NoArgsConstructor
class Student{
    private String name;
    private int age;
}
